package com.zrq.secondkill.service.impl;

import com.zrq.secondkill.dao.UserDOMapper;
import com.zrq.secondkill.dao.UserPasswordDOMapper;
import com.zrq.secondkill.dataobject.*;
import com.zrq.secondkill.error.BusinessErrorEnum;
import com.zrq.secondkill.error.BusinessException;
import com.zrq.secondkill.service.SimplelayeredCacheService;
import com.zrq.secondkill.service.UserService;
import com.zrq.secondkill.service.model.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    final
    UserDOMapper userDOMapper;

    final
    UserPasswordDOMapper userPasswordDOMapper;

    final
    SimplelayeredCacheService simplelayeredCacheService;

    private static final String USER_CACHE_PREFIX = "usercache";

    public UserServiceImpl(UserDOMapper userDOMapper, UserPasswordDOMapper userPasswordDOMapper, SimplelayeredCacheService simplelayeredCacheService) {
        this.userDOMapper = userDOMapper;
        this.userPasswordDOMapper = userPasswordDOMapper;
        this.simplelayeredCacheService = simplelayeredCacheService;
    }

    @Override
    public UserModel validateLogin(String telphone, String encrptPassword) throws BusinessException {
        UserDO userDO = userDOMapper.selectByTelphone(telphone);
        if (userDO == null) {
            throw new BusinessException(BusinessErrorEnum.USER_LOGIN_FAIL);
        }
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
        if (!StringUtils.equals(encrptPassword, userPasswordDO.getEncrptPassword())) {
            throw new BusinessException(BusinessErrorEnum.USER_LOGIN_FAIL);
        }
        UserModel userModel = convertUserModelFromUserDO(userDO);
        userModel.setEncrptPassword(null);
        return userModel;
    }

    @Override
    public UserModel getUserById(Integer userId) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(userId);
        return userDO == null ? null : convertUserModelFromUserDO(userDO);
    }

    @Override
    public UserModel getUserByIdInCache(Integer userId) {
        String cacheKey = USER_CACHE_PREFIX + userId;

        UserModel userModel = (UserModel) simplelayeredCacheService.get(cacheKey);
        if (userModel == null) {
            UserDO userDO = userDOMapper.selectByPrimaryKey(userId);
            userModel = userDO == null ? null : convertUserModelFromUserDO(userDO);
            simplelayeredCacheService.setK(cacheKey, userModel, 1, TimeUnit.MINUTES, 2);
        }
        return userModel;
    }

    @Override
    public Boolean validateUserId(Integer userId) {
        UserModel userModel = getUserByIdInCache(userId);
        return userModel!=null;
    }

    private static UserModel convertUserModelFromUserDO(UserDO userDO) {
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        return userModel;
    }
}
